package com.bestcem.xm.label.dao.impl.mysql;

import cn.hutool.core.collection.CollUtil;
import com.bestcem.xm.common.core.uitls.StringUtil;
import com.bestcem.xm.label.dao.LabelGroupDao;
import com.bestcem.xm.label.dao.mapper.LabelGroupMapper;
import com.bestcem.xm.label.entity.mongo.LabelGroup;
import com.bestcem.xm.label.entity.mysql.LabelGroupEntity;
import com.bestcem.xm.label.entity.pojo.LabelGroupDO;
import com.bestcem.xm.label.grpc.service.param.label.ListLabelsParam;
import com.bestcem.xm.label.service.dto.label.LabelInfoDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 基于 MongoDB数据源、MongoTemplate 实现的标签组数据访问接口
 *
 * @author dc.wang <dc.wang@idiaoyan.com>
 * @version V1.0
 * @date 2021/5/18
 */
@Slf4j
@Repository
public class LabelGroupDaoImpl implements LabelGroupDao {

    @Resource
    private LabelGroupMapper labelGroupMapper;

    @Override
    public boolean containsDuplicate(String orgId, String labelGroupName) {
        Integer count = labelGroupMapper.countByOrgIdAndName(orgId, labelGroupName);
        return count != 0;
    }

    @Override
    public Integer count(LabelGroupDO labelGroupEntity) {
        return labelGroupMapper.countByOrgIdAndMemberType(labelGroupEntity.getOrgId(), labelGroupEntity.getMemberType());
    }


    @Override
    public List<LabelGroupDO> list(String orgId) {
        List<LabelGroupEntity> entities = labelGroupMapper.findByOrgId(orgId);
        if (CollUtil.isNotEmpty(entities)) {
            return entities.stream().map(entity -> LabelGroupDO.toDo(entity)).collect(Collectors.toList());
        }
        return new ArrayList<>();
    }

    @Override
    public LabelGroupDO insert(LabelGroupDO labelGroupDO) {
        labelGroupDO.setId(StringUtil.getId());
        labelGroupMapper.saveLabelGroup(LabelGroupDO.toEntity(labelGroupDO));
        LabelGroupEntity entity = labelGroupMapper.findById(labelGroupDO.getId());
        return LabelGroupDO.toDo(entity);
    }

    @Override
    public LabelGroupDO updateNameById(LabelGroupDO labelGroupDO) {
        labelGroupMapper.updateNameById(labelGroupDO.getName(), labelGroupDO.getId(), labelGroupDO.getUpdateTime());
        LabelGroupEntity entity = labelGroupMapper.findById(labelGroupDO.getId());
        return LabelGroupDO.toDo(entity);
    }

    @Override
    public LabelGroupDO delete(String id) {
        LabelGroupEntity labelGroupEntity = labelGroupMapper.findById(id);
        int count = labelGroupMapper.deleteById(id);
        if (count == 0) {
            return null;
        }
        return LabelGroupDO.toDo(labelGroupEntity);
    }

    @Override
    public LabelGroupDO getById(String id) {
        LabelGroupEntity entity = labelGroupMapper.findById(id);
        return LabelGroupDO.toDo(entity);
    }

    @Override
    public List<LabelInfoDTO> listByParam(ListLabelsParam param) {
        return labelGroupMapper.listLabels(param.getOrgId(), param.getAttribute());
    }


    @Override
    public LabelGroupDO find(String orgId, String name) {
        LabelGroupEntity entity = labelGroupMapper.findByOrgIdAndName(orgId, name);
        return LabelGroupDO.toDo(entity);
    }

    @Override
    public List<LabelGroupDO> listByOrgId(String orgSrcId) {
        List<LabelGroupEntity> entities = labelGroupMapper.findByOrgId(orgSrcId);
        if (CollUtil.isNotEmpty(entities)) {
            return entities.stream().map(entity -> LabelGroupDO.toDo(entity)).collect(Collectors.toList());
        }
        return new ArrayList<LabelGroupDO>();
    }

    @Override
    public void batchSaveSampleData(List<LabelGroupDO> labelGroups) {
        if (CollUtil.isNotEmpty(labelGroups)) {
            List<LabelGroupEntity> labelGroupEntities =
                    labelGroups.stream().map(LabelGroupDO::toEntity).collect(Collectors.toList());
            labelGroupMapper.batchSaveSampleData(labelGroupEntities);
        }
    }

    @Override
    public Collection<LabelGroupDO> batchSave(Collection<LabelGroupDO> values) {
        if (CollUtil.isNotEmpty(values)) {
            List<LabelGroupEntity> labelGroupEntities = values.stream().map(item -> {
                item.setId(StringUtil.getId());
                return LabelGroupDO.toEntity(item);
            }).collect(Collectors.toList());
            labelGroupMapper.batchSaveLabelGroups(labelGroupEntities);
            return values;
        }
        return new ArrayList<>();
    }

    private LabelGroupEntity convert(LabelGroup labelGroup){
        LabelGroupEntity entity = new LabelGroupEntity();
        entity.setId(labelGroup.getId());
        if(labelGroup.getOrgId() == null){
            entity.setOrgId("");
        }else{
            entity.setOrgId(labelGroup.getOrgId());
        }
        entity.setName(labelGroup.getName());
        entity.setMemberType(labelGroup.getAttribute());
        entity.setSeq(labelGroup.getSerial());
        entity.setCreateTime(labelGroup.getCreateTime());
        if(labelGroup.getUpdateTime() == null){
            entity.setUpdateTime(labelGroup.getCreateTime());
        }else {
            entity.setUpdateTime(labelGroup.getUpdateTime());
        }
        return entity;
    }
}
